package au.com.betaversion;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import au.com.betaversion.domain.RegisteredUser;
import au.com.betaversion.repository.RegisteredUserRepository;

@Controller
public class RegisteredUserController {
	
	private static final String ROLE_USER = "ROLE_USER";
	
	@Autowired
	private MessageDigestPasswordEncoder messageDigestPasswordEncoder;
	
	@Autowired
	private RegisteredUserRepository registeredUserRepository;
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String registerForm(Model model) {
		model.addAttribute("registerForm", new RegisteredUser());
		return "register";
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public String registerFormSubmit(@ModelAttribute("registerForm") RegisteredUser registeredUser, Model model) {
		String encoded = messageDigestPasswordEncoder.encodePassword(registeredUser.getPassword(), null);
		registeredUser.setPassword(encoded);
		registeredUser.setRole(ROLE_USER);
		registeredUserRepository.save(registeredUser);

		return "redirect:/";
	}
	
	@RequestMapping(value = "/register/list", method = RequestMethod.GET)
	public String registerList(Model model) {
		List<RegisteredUser> users = (List<RegisteredUser>) registeredUserRepository.findAll();
		model.addAttribute("users", users);
		
		return "register/list";
	}
	
	@RequestMapping(value = "/register/delete/{username}")
	public String delete(@PathVariable("username") String username, Model model) {
		registeredUserRepository.delete(username);
		
		return "redirect:/register/list";
	}
	
	@RequestMapping(value = "/register/edit/{username}", method = RequestMethod.GET)
	public String edit(@PathVariable("username") String username, Model model) {
		model.addAttribute("registerForm", registeredUserRepository.findOne(username));
		
		return "register/edit";
	}
	
	@RequestMapping(value = "/register/edit/{username}", method = RequestMethod.POST)
	public String editUser(@ModelAttribute("registerForm") RegisteredUser registeredUser, Model model) {
		registeredUserRepository.save(registeredUser);
		return "redirect:/";
	}
	
	@RequestMapping(value = "/register/check", method = RequestMethod.POST)
	public @ResponseBody String checkUsername(@RequestParam("username") String username) {
		if(registeredUserRepository.findOne(username) != null) {
			return "taken";
		}
		return "free";
	}

}